﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AppAcc.Models;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Controls;


namespace AppAcc.MuaHang
{
    public partial class NhapHang : Form
    {
        private AppAccContext db = new AppAccContext();
        private int idtam = 0;
        public NhapHang()
        {
            InitializeComponent();
        }
        void ngay_gio()
        {
            int ngay = DateTime.Now.Day;
            int thang = DateTime.Now.Month;
            int nam = DateTime.Now.Year;
            txt_NgayLapPhieu.Text = ngay.ToString() + "/" + thang.ToString() + "/" + nam.ToString();
            txt_NgayChungTu.Text = ngay.ToString() + "/" + thang.ToString() + "/" + nam.ToString();

        }

        void GetMaPhieuNhanHang()
        {
            var t = db.PhieuNhapKhoHangs.Count();
            MessageBox.Show("PNH" + (t + 1).ToString());
        }
        void GetListDoiTac()
        {
            cbb_DoiTac.DataSource = db.DoiTacs.ToList();
            cbb_DoiTac.DisplayMember = "TenDoiTac";
            cbb_DoiTac.ValueMember = "Id";
            cbb_DoiTac.DropDownColumns = "Id,TenDoiTac";
            cbb_DoiTac.DropDownColumnsHeaders = "Mã đối tác\r\nTên đối tác\r\n";
        }

        void GetListNhanVien()
        {
            cbb_NhanVien.DataSource = db.NhanViens.ToList();
            cbb_NhanVien.DisplayMember = "TenNhanVien";
            cbb_NhanVien.ValueMember = "Id";
            cbb_NhanVien.DropDownColumns = "ID,TenNhanVien";
            cbb_NhanVien.DropDownColumnsHeaders = "Mã nhân viên\r\nTên nhân viên\r\n";
        }
        void getlistTaiKhoanNo()
        {
            DataGridViewComboBoxExColumn column = dvg_NhapHang.Columns["TKNo"] as DataGridViewComboBoxExColumn;
            //DataTable dt = new DataTable();
            //dt.Columns.Add(new DataColumn("MaTaiKhoan", typeof(string)));
            //dt.Columns.Add(new DataColumn("TenTaiKhoan", typeof(string)));
            //column.DataSource = db.TaiKhoans.ToList();
            //column.ValueMember = "MaTaiKhoan";
            //column.DisplayMember = "TenTaiKhoan";
            //dvg_NhapHang.Columns.Add(column);

            idtam = 0;
            var n = dvg_NhapHang.Columns["TKNo"] as DataGridViewComboBoxExColumn;
            n.DataSource = db.TaiKhoans.ToList();
            n.DisplayMember = "MaTaiKhoan";
            n.ValueMember = "MaTaiKhoan";
            //n.DropDownColumns = "ID,TenNhanVien";
            //n.DropDownColumnsHeaders = "Mã nhân viên\r\nTên nhân viên\r\n";
            n.DropDownWidth = 300;

        }



        void getlistTaiKhoanCo()
        {
            idtam = 0;
            var n = dvg_NhapHang.Columns["TKCo"] as DataGridViewComboBoxExColumn;
            n.DataSource = db.TaiKhoans.ToList();
            n.DisplayMember = "TenTaiKhoan";
            n.ValueMember = "MaTaiKhoan";
        }
        void getlistHangHoa()
        {
            idtam = 0;
            var n = dvg_NhapHang.Columns["HangHoaId"] as DataGridViewComboBoxExColumn;
            n.DataSource = db.HangHoas.ToList();
            n.DisplayMember = "TenHang";
            n.ValueMember = "Id";
        }
        void getlistKhoHang()
        {
            idtam = 0;
            var n = dvg_NhapHang.Columns["KhoHangId"] as DataGridViewComboBoxExColumn;
            n.DataSource = db.KhoHangs.ToList();
            n.DisplayMember = "TenKhoHang";
            n.ValueMember = "Id";
        }
        void getlistNhomThue()
        {
            idtam = 0;
            var n = dvg_NhapHang.Columns["NhomThueId"] as DataGridViewComboBoxExColumn;
            n.DataSource = db.NhomThueHangHoas.ToList();
            n.DisplayMember = "TenThue";
            n.ValueMember = "Id";
        }

        private void bt_Huy_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void FrmNhapHang_Load(object sender, EventArgs e)
        {
            ngay_gio();
            GetMaPhieuNhanHang();
            GetListNhanVien();
            GetListDoiTac();
            getlistTaiKhoanNo();
            getlistTaiKhoanCo();
            getlistHangHoa();
            getlistKhoHang();
            getlistNhomThue();
        }
        private void bt_ThemDoiTac_Click(object sender, EventArgs e)
        {
            Views.FormDoiTacThem a = new Views.FormDoiTacThem();
            a.ShowDialog();

        }

        void ThemMoiMuaHang()
        {
            decimal tongtien = 0;
            var PNK = new PhieuNhapKhoHang();
            PNK.NgayChungTu = Convert.ToDateTime(txt_NgayChungTu.Text);
            PNK.NgayLapPhieu = Convert.ToDateTime(txt_NgayLapPhieu.Text);
            PNK.NhanVienId = Convert.ToInt32(cbb_NhanVien.SelectedValue);
            PNK.NoiDung = txt_NoiDung.Text;
            PNK.DienGiai = txt_DienGiai.Text;
            PNK.DoiTacId = Convert.ToInt32(cbb_DoiTac.SelectedValue);
            PNK.TrangThai = 1;
            foreach (DataGridViewRow p in dvg_NhapHang.Rows)
            {
                var CT = new PhieuNhapKhoHangChiTiet();
                CT.TaiKhoanNo = Convert.ToString(p.Cells["TKNo"].Value);
                CT.TaiKhoanCo = Convert.ToString(p.Cells["TKCo"].Value);
                CT.HangHoaId = int.Parse(p.Cells["HangHoaId"].Value.ToString());
                CT.SoLuong = int.Parse(p.Cells["SoLuongNhap"].Value.ToString());
                CT.GiaNhap = decimal.Parse(p.Cells["DonGiaNhap"].Value.ToString());
                CT.SoTien = CT.SoLuong * CT.GiaNhap;
                CT.NhomThueHangHoaId = int.Parse(p.Cells["NhomThueId"].Value.ToString());
                CT.KhoHangId = int.Parse(p.Cells["KhoHangId"].Value.ToString());
                CT.DienGiai = txt_DienGiai.Text;
                tongtien = CT.SoLuong * CT.GiaNhap;
                var x = db.HangHoas.Find(CT.HangHoaId);
                x.SoLuong += CT.SoLuong;
                db.Entry(x).State = EntityState.Modified;
            }
            PNK.TongTien = tongtien;
            db.PhieuNhapKhoHangs.Add(PNK);
            try
            {
                db.SaveChanges();
                dvg_NhapHang.ClearSelection();
                dvg_NhapHang.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
        private void bt_Luu_Click(object sender, EventArgs e)
        {
            

        }

        private void cbb_DoiTac_SelectedIndexChanged(object sender, EventArgs e)
        {
            // KiemTra();

        }

        private void bt_NhanVien_Click(object sender, EventArgs e)
        {
            var a = new Views.FormNhanVienThem();
            a.ShowDialog();

        }

        private void cbb_NhanVien_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }


    }
}
